﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan.Utils
{
    public class KiemTraMaSo
    {
        private static PhieuChiTienManagement phieuchitienMgt = new PhieuChiTienManagement();
        private static PhieuThuTienManagement phieuthutienMgt = new PhieuThuTienManagement();
        private static MuaHangManagement muahangMgt = new MuaHangManagement();
        private static PhieuKeToanManagement phieuketoanMgt = new PhieuKeToanManagement();
        private static PhieuChuyenKhoManagement phieuchuyenkhoMgt = new PhieuChuyenKhoManagement();
        private static PhieuDoiHangManagement phieudoihangMgt = new PhieuDoiHangManagement();
        private static PhieuHangTraManagement phieuhangtraMgt = new PhieuHangTraManagement();
        private static PhieuNhapKhoMgt phieunhapkhoMgt = new PhieuNhapKhoMgt();
        private static PhieuXuatHangManagement phieuxuathangMgt = new PhieuXuatHangManagement();
        private static PhieuXuatKhoManagement PhieuXuatKhoMgt = new PhieuXuatKhoManagement();
        private static UyNhiemChiManagement uynhiemchiMgt = new UyNhiemChiManagement();
        private static BangKeNhapHangMgt bangkenhaphangMgt = new BangKeNhapHangMgt();
        private static DoiTruThueManagement doitruthueMgt = new DoiTruThueManagement();

        public static bool CheckOrderNumber(string prefix, string order, int id, DateTime dt)
        {
            bool isDuplicated = false;

            switch (id)
            {
                case 1:
                    List<BangKeNhapHang> lstBangKeNhapHang = GetAllBangKeNhapHangToCheckCode();
                    List<BangKeNhapHang> lstBK = lstBangKeNhapHang.Where(l => l.MaSo.StartsWith(prefix)).ToList();

                    //index = lstBK.Count == 0 ? 1 : lstBK[lstBK.Count - 1].MaSo.LastIndexOf('/');
                    //so = Convert.ToInt32(lstBK[lstBK.Count - 1].MaSo.Substring(index + 3));
                    //newMaSo = so + 1;

                    break;
                case 2:
                    List<MuaHang> lstMuaHang = GetAllMuaHang();
                    List<MuaHang> lstMH = lstMuaHang.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstMH.Count == 0 ? false : true;

                    break;
                case 3:
                    List<DoiTruThue> lstDoiTruThue = GetAllDoiTruThueToCheckCode();
                    List<DoiTruThue> lstDTT = lstDoiTruThue.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstDTT.Count == 0 ? false : true;

                    break;
                case 4:
                    List<PhieuDoiHang> lstPhieuDoiHang = GetAllPhieuDoiHang();
                    List<PhieuDoiHang> lstPDH = lstPhieuDoiHang.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstPDH.Count == 0 ? false : true;

                    break;
                case 5:
                    List<PhieuHangTra> lstPhieuHangTra = GetAllPhieuHangTra();
                    List<PhieuHangTra> lstPHT = lstPhieuHangTra.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstPHT.Count == 0 ? false : true;

                    break;
                case 6:
                    List<PhieuNhapKho> lstPhieuNhapKho = GetAllPhieuNhapKhoToCheckCode();
                    List<PhieuNhapKho> lstPNK = lstPhieuNhapKho.Where(l => l.MaSo.StartsWith(prefix)).ToList();

                    //index = lstPNK.Count == 0 ? 1 : lstPNK[lstPNK.Count - 1].MaSo.LastIndexOf('/');
                    //so = Convert.ToInt32(lstPNK[lstPNK.Count - 1].MaSo.Substring(index + 3));
                    //newMaSo = so + 1;

                    break;
                case 7:
                    List<PhieuXuatKho> lstPhieuXuatKho = GetAllPhieuXuatKho();
                    List<PhieuXuatKho> lstPXK = lstPhieuXuatKho.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstPXK.Count == 0 ? false : true;

                    break;
                case 8:
                    List<PhieuXuatHang> lstPhieuXuatHang = GetAllPhieuXuatHang();
                    List<PhieuXuatHang> lstPXH = lstPhieuXuatHang.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstPXH.Count == 0 ? false : true;

                    break;
                case 9:
                    List<PhieuChuyenKho> lstPhieuChuyenKho = GetAllPhieuChuyenKho();
                    List<PhieuChuyenKho> lstPCK = lstPhieuChuyenKho.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstPCK.Count == 0 ? false : true;

                    break;
                case 10:
                    List<PhieuKeToan> lstPhieuKeToan = GetAllPhieuKeToan();
                    List<PhieuKeToan> lstPKT = lstPhieuKeToan.Where(l => l.MaSo.StartsWith(prefix)).ToList();
                    isDuplicated = lstPKT.Count == 0 ? false : true;

                    break;
                case 11:
                    List<PhieuThuTien> lstPhieuThuTien = GetAllPhieuThuTien();
                    List<PhieuThuTien> lstPT = lstPhieuThuTien.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstPT.Count == 0 ? false : true;

                    break;
                case 12:
                    List<PhieuChiTien> lstPhieuChiTien = GetAllPhieuChiTien();
                    List<PhieuChiTien> lstPC = lstPhieuChiTien.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstPC.Count == 0 ? false : true;

                    break;
                case 13:
                    List<UyNhiemChi> lstUyNhiemChi = GetAllUyNhiemChi();
                    List<UyNhiemChi> lstUNC = lstUyNhiemChi.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year && l.OrderNumber == order).ToList();
                    isDuplicated = lstUNC.Count == 0 ? false : true;

                    break;
                default: break;
            }

            return isDuplicated;
        }

        public static string CheckCode(string prefix, int idLoaiPhieu, DateTime dt)
        {
            string sNewnumber = "";

            int newMaSo = CountRecord(prefix, idLoaiPhieu, dt);

            string sId = newMaSo.ToString();
            if (sId.Length == 1)
                sNewnumber = "0000" + sId;
            else if (sId.Length == 2)
                sNewnumber = "000" + sId;
            else if (sId.Length == 3)
                sNewnumber = "00" + sId;
            else if (sId.Length == 4)
                sNewnumber = "0" + sId;
            else
                sNewnumber = sId;

            return sNewnumber;
        }

        public static int CountRecord(string prefix, int id, DateTime dt)
        {
            int newMaSo = 0;
            int index = 0;
            int so = 0;

            switch (id)
            {
                case 1:
                    List<BangKeNhapHang> lstBangKeNhapHang = GetAllBangKeNhapHangToCheckCode();
                    List<BangKeNhapHang> lstBK = lstBangKeNhapHang.Where(l => l.MaSo.StartsWith(prefix)).ToList();

                    index = lstBK.Count == 0 ? 1 : lstBK[lstBK.Count - 1].MaSo.LastIndexOf('/');
                    so = Convert.ToInt32(lstBK[lstBK.Count - 1].MaSo.Substring(index + 3));
                    newMaSo = so + 1;

                    break;
                case 2:
                    List<MuaHang> lstMuaHang = GetAllMuaHang();
                    List<MuaHang> lstMH = lstMuaHang.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstMH.Count == 0 ? 1 : Convert.ToInt32(lstMH[lstMH.Count - 1].OrderNumber) + 1;

                    break;
                case 3:
                    List<DoiTruThue> lstDoiTruThue = GetAllDoiTruThueToCheckCode();
                    List<DoiTruThue> lstDTT = lstDoiTruThue.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstDTT.Count == 0 ? 1 : Convert.ToInt32(lstDTT[lstDTT.Count - 1].OrderNumber) + 1;

                    break;
                case 4:
                    List<PhieuDoiHang> lstPhieuDoiHang = GetAllPhieuDoiHang();
                    List<PhieuDoiHang> lstPDH = lstPhieuDoiHang.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstPDH.Count == 0 ? 1 : Convert.ToInt32(lstPDH[lstPDH.Count - 1].OrderNumber) + 1;

                    break;
                case 5:
                    List<PhieuHangTra> lstPhieuHangTra = GetAllPhieuHangTra();
                    List<PhieuHangTra> lstPHT = lstPhieuHangTra.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstPHT.Count == 0 ? 1 : Convert.ToInt32(lstPHT[lstPHT.Count - 1].OrderNumber) + 1;

                    break;
                case 6:
                    List<PhieuNhapKho> lstPhieuNhapKho = GetAllPhieuNhapKhoToCheckCode();
                    List<PhieuNhapKho> lstPNK = lstPhieuNhapKho.Where(l => l.MaSo.StartsWith(prefix)).ToList();

                    index = lstPNK.Count == 0 ? 1 : lstPNK[lstPNK.Count - 1].MaSo.LastIndexOf('/');
                    so = Convert.ToInt32(lstPNK[lstPNK.Count - 1].MaSo.Substring(index + 3));
                    newMaSo = so + 1;

                    break;
                case 7:
                    List<PhieuXuatKho> lstPhieuXuatKho = GetAllPhieuXuatKho();
                    List<PhieuXuatKho> lstPXK = lstPhieuXuatKho.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstPXK.Count == 0 ? 1 : Convert.ToInt32(lstPXK[lstPXK.Count - 1].OrderNumber) + 1;

                    break;
                case 8:
                    List<PhieuXuatHang> lstPhieuXuatHang = GetAllPhieuXuatHang();
                    List<PhieuXuatHang> lstPXH = lstPhieuXuatHang.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstPXH.Count == 0 ? 1 : Convert.ToInt32(lstPXH[lstPXH.Count - 1].OrderNumber) + 1;

                    break;
                case 9:
                    List<PhieuChuyenKho> lstPhieuChuyenKho = GetAllPhieuChuyenKho();
                    List<PhieuChuyenKho> lstPCK = lstPhieuChuyenKho.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstPCK.Count == 0 ? 1 : Convert.ToInt32(lstPCK[lstPCK.Count - 1].OrderNumber) + 1;

                    break;
                case 10:
                    List<PhieuKeToan> lstPhieuKeToan = GetAllPhieuKeToan();
                    List<PhieuKeToan> lstPKT = lstPhieuKeToan.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstPKT.Count == 0 ? 1 : Convert.ToInt32(lstPKT[lstPKT.Count - 1].OrderNumber) + 1;

                    break;
                case 11:
                    List<PhieuThuTien> lstPhieuThuTien = GetAllPhieuThuTien();
                    List<PhieuThuTien> lstPT = lstPhieuThuTien.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstPT.Count == 0 ? 1 : Convert.ToInt32(lstPT[lstPT.Count - 1].OrderNumber) + 1;

                    break;
                case 12:
                    List<PhieuChiTien> lstPhieuChiTien = GetAllPhieuChiTien();
                    List<PhieuChiTien> lstPC = lstPhieuChiTien.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstPC.Count == 0 ? 1 : Convert.ToInt32(lstPC[lstPC.Count - 1].OrderNumber) + 1;

                    break;
                case 13:
                    List<UyNhiemChi> lstUyNhiemChi = GetAllUyNhiemChi();
                    List<UyNhiemChi> lstUNC = lstUyNhiemChi.Where(l => l.Prefix == prefix && l.NgayThang.Month == dt.Month && l.NgayThang.Year == dt.Year).ToList();
                    newMaSo = lstUNC.Count == 0 ? 1 : Convert.ToInt32(lstUNC[lstUNC.Count - 1].OrderNumber) + 1;

                    break;
                default: break;
            }

            return newMaSo;
        }

        public static List<PhieuChiTien> GetAllPhieuChiTien()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuchitienMgt.GetAllPhieuChiTien();
            List<PhieuChiTien> listPhieuChiTien = (List<PhieuChiTien>)resultObj.Result;
            return listPhieuChiTien;
        }

        public static List<PhieuThuTien> GetAllPhieuThuTien()
        {
            ResultObj result = new ResultObj();
            result = phieuthutienMgt.GetAllPhieuThuTien();
            List<PhieuThuTien> lstPhieuthutien = (List<PhieuThuTien>)result.Result;
            return lstPhieuthutien;
        }

        public static List<MuaHang> GetAllMuaHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = muahangMgt.GetAllMuaHang();
            List<MuaHang> listMuaHang = (List<MuaHang>)resultObj.Result;
            return listMuaHang;
        }

        public static List<PhieuKeToan> GetAllPhieuKeToan()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuketoanMgt.GetAllPhieuKeToan();
            List<PhieuKeToan> listPhieuKeToan = (List<PhieuKeToan>)resultObj.Result;
            return listPhieuKeToan;
        }

        public static List<PhieuChuyenKho> GetAllPhieuChuyenKho()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuchuyenkhoMgt.GetAllPhieuChuyenKho();
            List<PhieuChuyenKho> listPhieuChuyenKho = (List<PhieuChuyenKho>)resultObj.Result;
            return listPhieuChuyenKho;
        }

        public static List<PhieuDoiHang> GetAllPhieuDoiHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieudoihangMgt.GetAllPhieuDoiHang();
            List<PhieuDoiHang> listPhieuDoiHang = (List<PhieuDoiHang>)resultObj.Result;
            return listPhieuDoiHang;
        }

        public static List<PhieuHangTra> GetAllPhieuHangTra()
        {
            ResultObj result = new ResultObj();
            result = phieuhangtraMgt.GetAllPhieuHangTra();
            List<PhieuHangTra> lstPhieuHangTra = (List<PhieuHangTra>)result.Result;
            return lstPhieuHangTra;
        }

        public static List<PhieuNhapKho> GetAllPhieuNhapKhoToCheckCode()
        {
            ResultObj result = new ResultObj();
            result = phieunhapkhoMgt.GetAllPhieuNhapKhoToCheckCode();
            List<PhieuNhapKho> lstPhieuNhapKho = (List<PhieuNhapKho>)result.Result;
            return lstPhieuNhapKho;
        }

        public static List<PhieuXuatHang> GetAllPhieuXuatHang()
        {
            ResultObj result = new ResultObj();
            result = phieuxuathangMgt.GetAllPhieuXuatHang();
            List<PhieuXuatHang> lstPhieuXuatHang = (List<PhieuXuatHang>)result.Result;
            return lstPhieuXuatHang;
        }

        public static List<PhieuXuatKho> GetAllPhieuXuatKho()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = PhieuXuatKhoMgt.GetAllPhieuXuatKho();
            List<PhieuXuatKho> listPhieuXuatKho = (List<PhieuXuatKho>)resultObj.Result;
            return listPhieuXuatKho;
        }

        public static List<UyNhiemChi> GetAllUyNhiemChi()
        {
            ResultObj result = new ResultObj();
            result = uynhiemchiMgt.GetAllUyNhiemChi();
            List<UyNhiemChi> lstUyNhiemChi = (List<UyNhiemChi>)result.Result;
            return lstUyNhiemChi;
        }

        public static List<BangKeNhapHang> GetAllBangKeNhapHangToCheckCode()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = bangkenhaphangMgt.GetAllBangKeNhapHangToCheckCode();
            List<BangKeNhapHang> listBangKeNhapHang = (List<BangKeNhapHang>)resultObj.Result;
            return listBangKeNhapHang;
        }

        public static List<DoiTruThue> GetAllDoiTruThueToCheckCode()
        {
            ResultObj result = new ResultObj();
            result = doitruthueMgt.GetAllDoiTruThue();
            List<DoiTruThue> lstDoiTruThue = (List<DoiTruThue>)result.Result;
            return lstDoiTruThue;
        }
    }
}
